﻿using System;
using System.Collections.Generic;
using System.Text;

using Castle.Core.Interceptor;
using Castle.Facilities.PolicyInjection;

namespace TestConsole.Policies.Handlers
{
    public class TestHandler
    {
        private Guid _guid;

        public TestHandler()
        {
            _guid = Guid.NewGuid();
        }

        public HandlerPrecallResult PreCall(HandlerContext handlerContext)
        {
            Console.Out.WriteLine("--- TestHandler " + _guid.ToString() + " PreCall ---");
            Console.Out.WriteLine("Method {0}", handlerContext.Invocation.Method.Name);
            return HandlerPrecallResult.Continue;
        }

        public void PostCall(HandlerContext handlerContext)
        {
            Console.Out.WriteLine("--- TestHandler " + _guid.ToString() + " PostCall ---");
            Console.Out.WriteLine("Method {0}", handlerContext.Invocation.Method.Name);
            if (handlerContext.Invocation.Method.ReturnType != typeof(void))
            {
                Console.Out.WriteLine("Return value: {0}", handlerContext.Invocation.ReturnValue.ToString());
            }
        }

        public HandlerOnExceptionResult OnException(HandlerContext handlerContext, Exception ex)
        {
            Console.Out.WriteLine("--- TestHandler " + _guid.ToString() + " OnException ---");
            Console.Out.WriteLine("Method {0}", handlerContext.Invocation.Method.Name);
            Console.Out.WriteLine("Exception: {0}", ex.Message);
            return HandlerOnExceptionResult.Block;
        }
    }
}
